home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / File.h < prev    next >
C/C++ Source or Header  |  1992-01-17  |  7KB  |  317 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _File_h 
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23. #define _File_h 1
  24.  
  25. #include <builtin.h>
  26. #include <stdio.h>
  27. #include <stddef.h>
  28.  
  29. #include <Fmodes.h>
  30.  
  31. class Filebuf;
  32.  
  33. class File
  34. {
  35.   friend class  Filebuf;
  36. protected:
  37.   FILE*         fp;              // _iobuf file pointer
  38.   char*         nm;              // file name (dynamically allocated)
  39.   char          rw;              //  1 = read; 2 = write; 3 = readwrite
  40.                                  //  bit 2 (4) means read/write into string
  41.   state_value   state;           // _good/_eof/_fail/_bad
  42.   long          stat;            // last read/write/... return value
  43.  
  44.   void          initialize();
  45.   void          reinitialize(const char*);
  46.   char         *readline (int chunk_number, char terminator);
  47.  
  48. public:
  49.                 File();
  50.                 File(const char* filename, io_mode m, access_mode a);
  51.                 File(const char* filename, const char* m);   
  52.                 File(int filedesc, io_mode m);
  53.                 File(FILE* fileptr);
  54.                 File(int sz, char* buf, io_mode m);
  55.  
  56.                 ~File();
  57.  
  58. // binding, rebinding, unbinding to physical files
  59.  
  60.   File&         open(const char* filename, io_mode m, access_mode a);
  61.   File&         open(const char* filename, const char* m);
  62.   File&         open(int  filedesc, io_mode m);
  63.   File&         open(FILE* fileptr);
  64.  
  65.   File&         close();
  66.   File&         remove();
  67.  
  68. // class variable access
  69.  
  70.   int           filedesc();
  71.   const char*   name();
  72.   void          setname(const char* newname);
  73.   int           iocount();
  74.  
  75.   int           rdstate();
  76.   int           eof();
  77.   int           fail();
  78.   int           bad();
  79.   int           good();
  80.  
  81. // other status queries
  82.  
  83.   int           readable();
  84.   int           writable();
  85.   int           is_open();
  86.  
  87.                 operator void*();
  88.  
  89. // error handling
  90.  
  91.   void          error();
  92.   void          clear(state_value f = _good); // poorly named
  93.   void          set(state_value f); // set corresponding but
  94.   void          unset(state_value f); // clear corresponding bit
  95.   File&         failif(int cond);
  96.   void          check_state();
  97.  
  98. // character IO
  99.  
  100.   File&         get(char& c);
  101.   File&         put(char  c);
  102.   File&         unget(char c);
  103.   File&         putback(char c); // a synonym for unget
  104.  
  105. // char* IO
  106.  
  107.   File&         put(const char* s);
  108.   File&         get    (char* s, int n, char terminator = '\n');
  109.   File&         getline(char* s, int n, char terminator = '\n');
  110.   File&         gets   (char **s, char terminator = '\n');
  111.  
  112. // binary IO
  113.  
  114.   File&         read(void* x, int sz, int n);
  115.   File&         write(const void* x, int sz, int n);
  116.  
  117. // formatted IO
  118.  
  119.   File&         form(const char* ...);
  120.   File&         scan(const char* ...);
  121.  
  122. // buffer IO
  123.  
  124.   File&         flush();
  125.   File&         flush(char ch); // call stdio _flsbuf
  126.   int           fill();         // call stdio _filbuf
  127.  
  128. // position control
  129.  
  130.   File&         seek(long pos, int seek_mode=0); // default seek mode=absolute
  131.   long          tell();
  132.  
  133. // buffer control
  134.  
  135.   File&         setbuf(int buffer_kind); // legal vals: _IONBF, _IOFBF, _IOLBF
  136.   File&         setbuf(int size, char* buf);
  137.   File&         raw();
  138. };
  139.  
  140.  
  141. // error handlers
  142.  
  143. extern void  verbose_File_error_handler(const char*);
  144. extern void  quiet_File_error_handler(const char*);
  145. extern void  fatal_File_error_handler(const char*);
  146. extern one_arg_error_handler_t File_error_handler;
  147. extern one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t);
  148.  
  149. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  150.  
  151.  
  152.  
  153. inline int File::filedesc()
  154.   return fileno(fp);
  155. }
  156.  
  157. inline const char* File::name()
  158.   return nm; 
  159. }
  160.  
  161. inline int File::iocount()
  162.   return stat; 
  163. }
  164.  
  165. inline void File::clear(state_value flag)
  166.   state = flag;
  167. }
  168.  
  169. inline void File::set(state_value flag)
  170.   state = state_value(int(state) | int(flag));
  171. }
  172.  
  173. inline void File::unset(state_value flag)
  174.   state = state_value(int(state) & ~int(flag));
  175. }
  176.  
  177. inline int File::readable()
  178.   if (fp != 0) { if (feof(fp)) set(_eof); if (ferror(fp)) set(_bad);}
  179.   return (state == _good && (rw & 01));
  180. }
  181.  
  182. inline int File::writable()
  183.   if (fp != 0 && ferror(fp)) set(_bad);
  184.   return ((int(state) & (int(_fail)|int(_bad))) == 0 && (rw & 02));
  185. }
  186.  
  187. inline int File::is_open()
  188.   return (fp != 0);
  189. }
  190.  
  191.  
  192. inline File& File::raw()
  193.   return this->File::setbuf(_IONBF); 
  194. }
  195.  
  196.  
  197. inline File& File::failif(int cond)
  198.   if (cond) set(_fail);  return *this; 
  199. }
  200.  
  201. inline File& File::get(char& c)
  202.   if (readable())
  203.   {
  204.     int ch = getc(fp);
  205.     c = ch;
  206.     failif (ch == EOF);
  207.   }
  208.   return *this;
  209. }
  210.  
  211. inline File& File::put(char  c) 
  212.   return failif (!writable() ||  putc(c, fp) == EOF);
  213. }
  214.  
  215. inline File& File::unget(char c)
  216.   return failif(!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  217.  
  218. inline File& File::putback(char c)
  219.   return failif (!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  220. }
  221.  
  222. inline File& File::read(void* x, int sz, int n)
  223.   return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
  224.  
  225. inline File& File::write(void* x, int sz, int n) 
  226.   return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
  227. }
  228.  
  229. inline File& File::flush()
  230.   return failif(!is_open() || fflush(fp) == EOF);
  231. }
  232.  
  233. inline File& File::flush(char ch)
  234. #ifdef VMS
  235.   return failif(!is_open() || c$$flsbuf(ch, fp) == EOF);
  236. #else
  237.   return failif(!is_open() || _flsbuf(ch, fp) == EOF);
  238. #endif
  239. }
  240.  
  241. inline int File::fill()
  242. #ifdef VMS
  243.   failif(!is_open() || (stat = c$$filbuf(fp)) == EOF);
  244. #else
  245.   failif(!is_open() || (stat = _filbuf(fp)) == EOF);
  246. #endif
  247.   return stat;
  248. }
  249.  
  250. inline File& File::seek(long pos, int seek_mode)
  251.   return failif (!is_open() || fseek(fp, pos, seek_mode) < 0); 
  252. }
  253.  
  254. inline long File::tell()
  255.   failif (!is_open() || ((stat = ftell(fp)) < 0));
  256.   return stat;
  257. }
  258.  
  259. inline int File::rdstate()
  260.   check_state();  return state; // check_state is necessary in rare but
  261. }                               // possible circumstances
  262.  
  263. inline File::operator void*()
  264.   check_state();  return (int(state) & (int(_bad)|int(_fail)))? 0 : this ; 
  265. }
  266.  
  267. inline int File::eof()
  268.   check_state(); return state & _eof; 
  269. }
  270.  
  271. inline int File::fail()
  272.   check_state(); return state & _fail; 
  273. }
  274.  
  275. inline int File::bad()
  276.   check_state(); return state & _bad; 
  277. }
  278.  
  279. inline int File::good()
  280.   check_state(); return rdstate() == _good; 
  281. }
  282.  
  283.  
  284. #endif
  285.  
  286. #endif
  287.